package com.ww.gss.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.ww.gss.vo.GSSTokenVO;
import com.ww.gss.vo.param.GSSParamVO;

public class GSSWebUtil {
    private static Logger logger = LoggerFactory.getLogger(GSSWebUtil.class);

    // 重定向请求
    public static void redirectRequest(String path, HttpServletResponse response) {
        try {
            response.sendRedirect(path);
        } catch (Exception e) {
            logger.error("重定向请求出错！", e);
            throw new RuntimeException(e);
        }
    }

    // 发送错误代码
    public static void sendError(int code, HttpServletResponse response) {
        try {
            response.sendError(code);
        } catch (Exception e) {
            logger.error("发送错误代码出错！", e);
            throw new RuntimeException(e);
        }
    }

    // 判断是否为 AJAX 请求
    public static boolean isAjax(HttpServletRequest request) {
        String headeraccept = request.getHeader("accept");
        String ajaxrequest = request.getHeader("X-Requested-With");

        return ((!GSSSystemUtils.isStrNull(headeraccept) && headeraccept.indexOf("application/json") > -1)
                || (!GSSSystemUtils.isStrNull(ajaxrequest) && ajaxrequest.indexOf("XMLHttpRequest") > -1));
    }

    public static void sendJson4Error(String errorcode, String messagestr, HttpServletResponse response) {
        Map respons = buildResponseMap(errorcode, null, messagestr);
        String json = JSON.toJSONString(respons);
        try {
            response.getWriter().print(json);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static Map buildResponseMap(String successcode, Object data, String message) {
        Map response = new HashMap();
        response.put("ErrorCode", successcode);
        if (SystemConstans.RESPONSE_LABEL_SUCCESS.equals(successcode)) {
            response.put("message", message);
        } else {
            response.put("error", message);
        }
        response.put("data", data);
        return response;
    }

    public static GSSTokenVO getTokenInRequest(HttpServletRequest request) {
        // TODO Auto-` method stub
        String token = "";
        GSSTokenVO vo = new GSSTokenVO();
        try {
            ServletInputStream inputstream = request.getInputStream();
            List<String> strlist = IOUtils.readLines(inputstream, SystemConstans.ENCODING_UTF8);
            StringBuilder sb = new StringBuilder();
            for (String line : strlist) {
                sb.append(line);
            }

            logger.info("--------------------input parameter---------------------------");
            logger.info(sb.toString());
            // BtmuParamVO paramvo = (BtmuParamVO) JSON.parse(sb.toString());
         //   BtmuParamVO paramvo = JSON.parseObject(sb.toString(), BtmuParamVO.class);
      //      token = paramvo.getAuthorization();
      //      String clientid = paramvo.getClientID();

         //   vo.setToken(token);
            logger.info("--------------------get token ---------------------------" + token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }

}
